
import Layout from '@/page/Layout'
import { getMenu } from '@/api/login'
import { refactoringArr } from "@/util/common";
import RouteView from '@/page/RouteView'
import notfount from '@/views/Exception/404'

const constantRouterComponents = {
  RouteView: RouteView,
  Layout: Layout,

  'Exc403': () => import('@/views/Exception/403'),
  'Exc404': () => import('@/views/Exception/404'),
  'Exc500': () => import('@/views/Exception/500'),

  // 首页
  'Home': () => import('@/views/Home'),

  // 企业管理
  'Company_List': () => import('@/views/Company/Companys/index'),
  'Company_List_Detail': () => import('@/views/Company/Companys/Detail/index'),
  'Company_Users': () => import('@/views/Company/Users/index'),
  'Company_Users_Detail': () => import('@/views/Company/Users/Detail'),
  'Company_Seal': () => import('@/views/Company/Seal/index'),

  //订单管理
  'Order': () => import('@/views/Order/index'),
  'Order_Detail': () => import('@/views/Order/Detail'),

  //系统管理
  'System_Authority': () => import('@/views/System/Authority'),
  'System_Department': () => import('@/views/System/Department'),
  'System_Member': () => import('@/views/System/Member'),

  //商城系统管理
  'MallSystem_Role': () => import('@/views/MallSystem/Role'),
  'MallSystem_DefaultPermission': () => import('@/views/MallSystem/DefaultPermission'),
  'MallSystem_PermissionResource': () => import('@/views/MallSystem/PermissionResource'),
  'MallSystem_WhitelistResource': () => import('@/views/MallSystem/WhitelistResource'),

  //资讯管理
  'Information_List': () => import('@/views/Information/index'),
  'Information_Release': () => import('@/views/Information/Release'),

  //积分管理
  'Integral_Order': () => import('@/views/Integral/Order'),
  'Integral_Exchange': () => import('@/views/Integral/Exchange'),
  'Integral_GuidePrice': () => import('@/views/Integral/GuidePrice'),
  'Integral_Detail': () => import('@/views/Integral/Detail'),
  'Integral_Add': () => import('@/views/Integral/Add'),
  'Integral_Summary': () => import('@/views/Integral/Summary'),

  //轮播管理
  'Carousel_list': () => import('@/views/Carousel/index'),

  //意见反馈
  'Feedback_list': () => import('@/views/Feedback/index'),
  
  //套保管理
  'Hedging_Account': () => import('@/views/Hedging/Account'),
  'Hedging_AddressOfPre': () => import('@/views/Hedging/AddressOfPre'),
  
  //线路管理
  'Line_list': () => import('@/views/Line/index'),

  //合同模板管理
  'Contract_Template': () => import('@/views/ContractTemplate/index'),

  //广告管理
  'Announcement_list': () => import('@/views/Announcement/index'),

  //系统日志
  'Log_Statistics': () => import('@/views/Log/Statistics'),
  'Log_Detail': () => import('@/views/Log/Detail'),

  //个人中心
  'UserInfo': () => import('@/views/User/Info'),
  
  //test
  'Test': () => import('@/views/Test/index'),
}

// 页面错误
const notFoundRouter = [
  {
    path: "/404",
    name: "Exc404",
    component: notfount
    }, 
    {
    path: "*",
    redirect: "/404"
    }
]

// 根级菜单
const rootRouter = {
  name: '',
  path: '',
  component: 'Layout',
  redirect: '/home',
  title: 'title',
  icon: 'home',
  children: []
}

/**
 * 动态生成菜单
 * @param token
 * @returns {Promise<Router>}
 */
export const generatorDynamicRouter = (token) => {
  return new Promise((resolve, reject) => {
    getMenu(token).then(res => {
      const menuNav = []
      let tree = []
      tree = refactoringArr(res.data, "", 0)
      rootRouter.children = tree
      menuNav.push(rootRouter)
      const routers = generator(menuNav)
      const finalRouter = routers.concat(notFoundRouter) 
      resolve(finalRouter)
    }).catch(err => {
      reject(err)
    })
  })
}

/**
 * 格式化树形结构数据 生成 vue-router 层级路由表
 *
 * @param routerMap
 * @param parent
 * @returns {*}
 */
export const generator = (routerMap) => {

  return routerMap.map(item => {
    const currentRouter = {
      // ...item,
      path: item.path || '',
      name: item.resourceName || item.name,
      component: (constantRouterComponents[item.moduleUrl || item.component]),
      id: item.id,
      icon: item.icon || '',
      title: item.flag || item.title,
      hidden: item.hidden || '0',
      parentId: item.parentId
    }
    item.redirect && (currentRouter.redirect = item.redirect)
    if (item.children && item.children.length > 0) {
      currentRouter.children = generator(item.children)

    }
    return currentRouter
  })
}
